﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BattleShips.Common;
using TestServer;

namespace BattleShips.GameServers.CommandRunners {
    class HitField : ICommandRunner {
        public string RunCommand(Command command, GameClient client, GameServer server) {
            string fieldToHit = command.Argument;
            bool CanSetPlayerTurn = true;
            Dictionary<uint, GameRoom> rooms = new Dictionary<uint, GameRoom>();
            rooms = server.GetRooms();
            foreach (var item in rooms[client.InRoomId].GetRoomClients().Values) {
                if (item != client) {
                    if (item.clientGameState.selectedFields.Contains(fieldToHit)) {
                        if (!client.clientGameState.hittedFields.Contains(fieldToHit)) {
                            server.HitChangePanel1(item, fieldToHit);
                            server.HitChangePanel2(client, fieldToHit);
                            client.clientGameState.hittedFields.Add(fieldToHit);
                            if (client.clientGameState.hittedFields.Count == 3) {
                                System.Threading.Thread.Sleep(500);
                                server.GameEnd(client, item, "gameend");
                                rooms[client.InRoomId].GameIsEnd = true;
                            } else if (item.clientGameState.hittedFields.Count == 3) {
                                System.Threading.Thread.Sleep(500);
                                server.GameEnd(item, client, "gameend");
                                rooms[client.InRoomId].GameIsEnd = true;
                            }
                        } else {
                            Response msg = new Response();
                            msg.CommandName = "msg";
                            msg.Payload = "You already hit that field.";
                            msg.Code = 0;
                            client.SendMessage(msg.ToString());
                            CanSetPlayerTurn = false;
                        }
                    } else {
                        if (!client.clientGameState.missFields.Contains(fieldToHit)) {
                            server.MissChangePanel1(item, fieldToHit);
                            server.MissChangePanel2(client, fieldToHit);
                            client.clientGameState.missFields.Add(fieldToHit);
                        } else {
                            Response msg = new Response();
                            msg.CommandName = "msg";
                            msg.Payload = "You already miss that field.";
                            msg.Code = 0;
                            client.SendMessage(msg.ToString());
                            CanSetPlayerTurn = false;
                        }
                    }
                    if (!rooms[client.InRoomId].GameIsEnd && CanSetPlayerTurn == true) {
                        server.SetPlayerTurn(rooms[client.InRoomId].idRoom, item);
                    }
                }
            }
            return fieldToHit;
        }
    }
}
